package com.icinfo.cloud.provider.punish.common.datareport.controller;

import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.punish.common.datareport.enums.ReportDataTypeEnum;
import com.icinfo.cloud.provider.punish.common.datareport.mapper.CaseDataReportRecordMapper;
import com.icinfo.cloud.provider.punish.common.datareport.model.CaseDataReportRecord;
import com.icinfo.cloud.provider.punish.common.datareport.service.ICaseDataReportRecordService;
import com.icinfo.framework.common.web.Result;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 描述：处罚决定上报成功记录处理前端控制器
 *
 * @Author zhengqiang
 * @Date 2022/7/27 9:26
 **/
@Slf4j
@RestController
@RequestMapping("/punish/historyDecision")
public class HistoryDecisionReportUpdateController {

    @Resource
    private CaseDataReportRecordMapper caseDataReportRecordMapper;
    @Resource
    private ICaseDataReportRecordService caseDataReportRecordService;

    /**
     * 描述： 更新历史行政处罚决定上报成功记录的公示时间及地址
     *
     * @author: zhengqiang
     * @date: 2022/7/27
     * @return
     **/
    @RequestMapping("/updateHistoryDecisionReports")
    public Result updateHistoryDecisionReports() {
        log.info("处罚决定上报记录的公示时间及地址更新开始处理");
        Example report = new Example(CaseDataReportRecord.class);
        report.createCriteria().andEqualTo("reportDataType", ReportDataTypeEnum.DECISION.getCode())
                .andEqualTo("reportResult", Constant.STRING_0);
        report.orderBy("createTime").desc();

        List<CaseDataReportRecord> caseDataReportRecords = caseDataReportRecordMapper.selectByExample(report);
        if (ObjectUtils.isEmpty(caseDataReportRecords)) {
            return Result.success("无处罚决定上报历史记录");
        }

        caseDataReportRecords.forEach(record -> {
            try {
                int i = caseDataReportRecordService.updateHistoryDecisionReport(record);
                if (i == 0) {
                    log.error("处罚决定上报记录的公示时间及地址更新失败--caseId:" + record.getCaseId());
                }
            } catch (Exception e) {
                e.printStackTrace();
                log.error("处罚决定上报记录的公示时间及地址更新失败", e);
            }
        });
        log.info("处罚决定上报记录的公示时间及地址更新开始处理结束");
        return Result.success("处罚决定上报历史记录的公示时间及地址更新处理结束");
    }

}
